//
//  CYSingleTicketCell.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/8/18.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYSingleTicketCell.h"
@interface CYSingleTicketCell()
@property (nonatomic, strong) CYLabel *dateLBL;//期号
@property (nonatomic, strong) CYLabel *timeLBL;//时间
@property (nonatomic, strong) CYView *greenView;//快三系列，骰子背景图
@property (nonatomic, strong) CYLabel *sumLBL;//和值
@property (nonatomic, copy) NSArray *winNumArr;//开奖号码
@end
@implementation CYSingleTicketCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.isFirst = NO;
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        [self setSeparatorInset:UIEdgeInsetsZero]; /*cell的分割线，左对齐*/
        [self setLayoutMargins:UIEdgeInsetsZero];  /*cell的分割线，左对齐*/
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

/*懒加载*/
- (CYLabel *)dateLBL{
    if (!_dateLBL) {
        _dateLBL = [[CYLabel alloc] initWithText:nil size:14 textcolor:ColorHex(0x333333)];
        _dateLBL.justSizeToFit = YES;
        [self addSubview:_dateLBL];
        
        [_dateLBL mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(10);
            make.top.equalTo(self).with.offset(5);
            make.size.mas_equalTo(_dateLBL.bounds.size);
        }];
    }
    return _dateLBL;
}

- (CYLabel *)timeLBL{
    if (!_timeLBL) {
        _timeLBL = [[CYLabel alloc]initWithText:nil size:11 textcolor:ColorHex(0x888888)];
        _timeLBL.justSizeToFit = YES;
        [self addSubview:_timeLBL];
        
        [_timeLBL mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_dateLBL.mas_right).with.offset(AUTO(10));
            make.centerY.equalTo(_dateLBL);
            make.size.mas_equalTo(_timeLBL.bounds.size);
        }];
    }
    return _timeLBL;
}

- (CYView *)greenView{
    if (!_greenView) {
        _greenView = [[CYView alloc]initWithColor:RGBA(30, 194, 130, 1)];
        _greenView.bounds = CGRectMake(0.0, 0.0, AUTO(30) * 4, 30);
        _greenView.layer.cornerRadius = _greenView.bounds.size.height / 2.0;
        _greenView.layer.masksToBounds = YES;
        [self addSubview:_greenView];
        
        [_greenView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_dateLBL.mas_bottom).with.offset((100 - CGRectGetMaxY(_dateLBL.bounds) - 30) / 2.0 - 2);
            make.left.mas_equalTo(_dateLBL);
            make.size.mas_equalTo(_greenView.bounds.size);
        }];
    }
    return _greenView;
}

- (CYLabel *)sumLBL{
    if (!_sumLBL) {
        _sumLBL = [[CYLabel alloc]initWithText:[NSString stringWithFormat:@"和值:%d",[_winNumArr sum]] size:13 textcolor:ColorHex(0x333333)];
        _timeLBL.justSizeToFit = YES;
        [self addSubview:_sumLBL];
        
        [_sumLBL mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_greenView);
            make.left.equalTo(_greenView.mas_right).with.offset(AUTO(30));
            make.size.mas_equalTo(_sumLBL.bounds.size);
        }];
    }
    return _sumLBL;
}

- (void)setWinNumModel:(CYWinNumberModel *)winNumModel{
    if (_winNumModel != winNumModel) {
        _winNumModel = winNumModel;
        self.winNumArr = [_winNumModel.data componentsSeparatedByString:@","];
        self.dateLBL.text = [NSString getDateNumber:_winNumModel.actionNo];
        self.timeLBL.text = _winNumModel.actionDateTime;
        [self clearSubview];
        [self setupSubview];
    }
}

/*设置子视图的布局*/
- (void)setupSubview{
    
    if ([_lotteryName containsString:@"快三"]) {
        self.greenView.hidden = self.sumLBL.hidden = NO;
        self.sumLBL.text = [NSString stringWithFormat:@"和值:%d",[_winNumArr sum]];
        self.greenView.backgroundColor =(_isFirst)?RGBA(30, 194, 130, 1):[UIColor clearColor];
        
        [self.greenView mas_updateConstraints:^(MASConstraintMaker *make) {
            if (_isFirst)  make.top.mas_equalTo(_dateLBL.mas_bottom).with.offset((100 - CGRectGetMaxY(_dateLBL.bounds) - 30) / 2.0 - 2);
            else  make.top.mas_equalTo(_dateLBL.mas_bottom).with.offset((60 - CGRectGetMaxY(_dateLBL.bounds) - 30) / 2.0 - 2);
        }];
        
        for (int i = 0 ; i < _winNumArr.count; i++) {
            NSString *title = [NSString stringWithFormat:@"%dse",[_winNumArr[i] intValue]];
            CYImageView *imageView = [[CYImageView alloc]initWithImageName:title size:CGSizeMake(25, 25) isRadius:NO];
            imageView.tag = 400 + i;
            [self.greenView addSubview:imageView];
            
            CGFloat offset_width = (_greenView.bounds.size.width - imageView.bounds.size.width * _winNumArr.count) / 4.0;
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(_greenView);
                make.left.equalTo(_greenView).with.offset(offset_width + (imageView.bounds.size.width + offset_width) * i);
                make.size.mas_equalTo(imageView.bounds.size);
            }];
        }
    }
    else {
        self.greenView.hidden = self.sumLBL.hidden = YES;
        
        for (int i = 0 ; i < _winNumArr.count; i++) {
            CGFloat radius = (_winNumArr.count < 10)?30:25;
            CGFloat offset_width = (SCREEN_WIDTH - _winNumArr.count * radius - 100) * 1.0 / (_winNumArr.count + 1);
            CGFloat font = (_winNumArr.count < 10)?14:11;//
            CGFloat height = (_isFirst)?100:60;//如果是第一行，则高度为100,其余，高度为60
            CYLabel *label = [[CYLabel alloc]initWithText:_winNumArr[i] size:font weight:UIFontWeightBold];
            label.backgroundColor = (_isFirst)?THEME_COLOR:[UIColor whiteColor];
            label.textColor = (_isFirst)?[UIColor whiteColor]:THEME_COLOR;
            label.layer.cornerRadius = radius / 2.0;
            label.layer.masksToBounds = YES;
            label.textAlignment = NSTextAlignmentCenter;
            label.tag = 200 + i;
            [self addSubview:label];
            
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(_dateLBL.mas_bottom).with.offset((height - CGRectGetMaxY(_dateLBL.bounds) - 30) / 2.0 - 2);
                make.left.equalTo(_dateLBL).with.offset((radius + offset_width) * i);
                make.size.mas_equalTo(CGSizeMake(radius, radius));
            }];
        }
    }
}

- (void)clearSubview{
    
    NSArray *array = [self subviews];
    for (int i = 0; i < array.count; i ++) {
        CYView *view = array[i];
        if (view.tag >= 200)
            [view removeFromSuperview];
    }
    
    for (CYImageView *imageView in [_greenView subviews]) {
        [imageView removeFromSuperview];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
